package com.jun.seckill_by_jun.service;

import com.jun.seckill_by_jun.dao.OrderDao;
import com.jun.seckill_by_jun.domain.OrderInfo;
import com.jun.seckill_by_jun.domain.SeckillOrder;
import com.jun.seckill_by_jun.domain.SeckillUser;
import com.jun.seckill_by_jun.redis.OrderKey;
import com.jun.seckill_by_jun.redis.RedisService;
import com.jun.seckill_by_jun.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private RedisService redisService;
    public SeckillOrder getOrderByUserIdGoodsId(long id, long goodsId) {
        return redisService.get(OrderKey.seckillOrder,"uid_gid",SeckillOrder.class);
//        return orderDao.getOrderByUserIdGoodsId(id, goodsId);
    }
    @Transactional
    public OrderInfo createOrder(SeckillUser user, GoodsVo goods) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setDeliveryAddrId(0L);
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsId(goods.getId());
        orderInfo.setGoodsName(goods.getGoodsName());
        orderInfo.setGoodsPrice(goods.getGoodsPrice());
        orderInfo.setOrderChannel(1);
        orderInfo.setStatus(0);
        orderInfo.setUserId(user.getId());
        orderDao.insert(orderInfo);

        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setGoodsId(goods.getId());
        seckillOrder.setOrderId(orderInfo.getId());
        seckillOrder.setUserId(user.getId());
        orderDao.insertSeckillOrder(seckillOrder);

        redisService.set(OrderKey.seckillOrder,"uid_gid",seckillOrder);
        return orderInfo;
    }

    public OrderInfo getOrderById(long orderId) {
        OrderInfo order = orderDao.getOrderByOrderId(orderId);
        return order;
    }
}
